package com.five.community.controller;

import com.five.community.bean.Car;
import com.five.community.bean.Person;
import com.five.community.bean.vo.CarVo;
import com.five.community.service.CarService;
import com.five.community.service.PersonService;
import com.five.community.util.FileUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private PersonService personService;

    @RequestMapping("/listUi")
    public String listUi(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         Model model, ModelMap map){
        try {

            List<CarVo> pages= carService.selectAllCars(pageNum,pageSize);
            List<CarVo> cars = carService.selectAllCar();
            PageInfo<CarVo> pageInfo = new PageInfo<>(pages);
            model.addAttribute("pageInfo",pageInfo);
            map.addAttribute("cars",cars);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "car";
    }

    //添加页面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Person> persons = personService.selectAll();
            map.addAttribute("persons", persons);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "car_add";
    }

    //添加方法
    @RequestMapping("/add")
    public String add(CarVo carVo, MultipartFile photos) {
        long id1 = Long.parseLong(carVo.getPname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        carVo.setPicture(fileName);
        carVo.setCreateTime(new Date());
        try {
            Car car = new Car(fileName, id1, carVo.getColor(), carVo.getNumber(),
                    carVo.getRemark(), new Date());
            carService.insert(car);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/car/addUi";
    }

    //删除方法
    @RequestMapping("/delete")
    public String delete(long id) {
        try {
            System.out.println("id = " + id);
            carService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/car/listUi";
    }

    //搜索功能
    @RequestMapping("/select")
    public String select(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                         String name, ModelMap map,Model model) {
        if (name == "") {
            return "redirect:/car/listUi";
        } else {
            try {
                List<CarVo> pages= carService.selectAllByName(pageNum,pageSize,name);
                List<CarVo> cars = carService.selectByName(name);
                PageInfo<CarVo> pageInfo = new PageInfo<>(pages);
                model.addAttribute("pageInfo",pageInfo);
                map.addAttribute("cars", cars);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "car";
        }
    }
    //更新页面
    @RequestMapping("/editUi")
    public String editUi(long id, ModelMap map1){
        try {
            Car car = carService.selectById(id);
            List<Person> person = personService.selectAll();
            map1.addAttribute("person",person);
            map1.addAttribute("car",car);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "car_edit";
    }
    //更新方法
    @RequestMapping("/edit")
    public String edit(CarVo carVo, MultipartFile photos) {
        long id1 = Long.parseLong(carVo.getPname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        carVo.setPicture(fileName);
        carVo.setCreateTime(new Date());
        try {
            Car car = new Car(carVo.getId(),fileName, id1, carVo.getColor(), carVo.getNumber(),
                    carVo.getRemark(), new Date());
            carService.update(car);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "car_edit";
    }
}
